export function waitFor(selector, callback, times = Number['MAX_SAFE_INTEGER'] || Number.MAX_VALUE, delay) {
	let cb = delay ? () => setTimeout(callback, delay) : callback;
	if (times <= 0) {
		console.warn('times up');
		cb(false);
	}
	let judger = typeof selector == 'function' ? selector : function() {
		return !!$(selector).length;
	}
	if (judger()) {
		cb(true);
	} else {
		console.log('not found ' + selector + ', wait for 1s...');
		setTimeout(function() {
			waitFor(selector, callback, times - 1, delay);
		}, 500);
	}
}

export function addResourceUrlProtocol(str, protocol = "https") {
	return str.replace(/(\w+=["'])?(\/\/)(\w+)/g, '$1' + protocol + ':$2$3')
}

export function getPageContentByUrl(url) {
	return $.get(url).then(resp => resp);
}


export function generateJqueryCallback() {
	return 'jQuery' + (new Date()).getTime();
}

export function generateTimestamp() {
	return (new Date()).getTime();
}

export function readCookie(a) {
	var b = a + "=";
	var c = document.cookie.split(";");
	for (var d = 0; d < c.length; d++) {
		var e = c[d];
		for (;
			" " == e.charAt(0);)
			e = e.substring(1, e.length);
		if (0 == e.indexOf(b))
			return e.substring(b.length, e.length)
	}
	return null
}
export function arrObjectCompare(e) {
	return function(t, n) {
		var i = t[e]
			, s = n[e];
		return i < s ? 1 : i > s ? -1 : 0
	}
}
export function pageHTML2JQueryObj(html, jq){
	let body = html.match(/<body .*?>([\s\S]*)<\/body>/i)[1];
	return jq(body);
}
